'use strict';

const Controller = require('egg').Controller;
// 发送支付功能
const AlipaySDK = require('alipay-sdk').default
// 将支付信息 formdata 包裹住
const AlipayFormData = require('alipay-sdk/lib/form').default

class PayController extends Controller {
    async alipay() {
        const { ctx, config } = this;
        const { total } = ctx.request.body
        // console.log(ctx.query.params);
        const alipaySdk = new AlipaySDK({
            appId: '2021000118645860', // 你自己的沙箱环境的appId
            privateKey: "MIIEowIBAAKCAQEAgvssaeiTU3u0U9cpoRY2dHv0iv+ogw/vYHPhkxU5FbdGbpUk+Pqyo8IZbAZgOFZO/cOvW9zrH05DyJxB0LLvePImL1iK8xgjR14EUQ3mx9Tv2l0pXQUBKTWiCQlSgK0cTQhfZOWH4WzeFgUcgXTBSJSfSPeX4soS9VErFLhrmnT2BT8EBgtH6MN3y78mTIWK19TgThAJE+s8DPqN/1K2DenhXuSBU/7KJVURNWdGPQRKyVjTIO4WgjTONHC/LAykVjyLSQO9bB7hUKxDrNl2ZHYyK7i6N5spsiEd3mXwCFPxZVtiPcm6PcQ+T1p3xQ+YAmQ9YlrNEl0f+dsMyDqGJQIDAQABAoIBAAkJn7TnEVNPU8/9Jqgwyi0baHI21b/FV7UGCeiGLFwtOzTiIZz0X2MhP3POtNDuU6D64ZyrD6YnVlH/RiNKVW+49Z2KtHu1jKmd+RpGpUfgj3Q3vWTntX7MbpPFCuZKC4vdKgfiNo/9XQ2T6LWeAsApURxNyTMi0lAO+7QATZxonvQvpvdsrZQmFl8S/mm3IgIZDDYhAilp0uDxojfPzJoigFb4ZD9yD9MTNq97kO5S4/s1POyjdgCq+bleNkFf9xlG/zMWldUnXiPqhDp4L6/2YwsUoEO6iWhz/kxOqliK/po5jb6Rvo+//Lg62vg9qP7Gn6tGvGDRvoU2PXWOf/kCgYEA/kdMIHsH3AosswaVBp5cs2kL8A10WHg4RJF4Elx70INLcqLZWv46ZdP9JOBuB3z8lNkDzJBNfgnJBly82dyuQ0fdfMB3A3rWNU2hasSZUjhrKMt6AzaVA5+h9/yjqRUfhCCotyh24YcomfKe5I/Q7PsYX+b8bHEks8O1w9DNdGsCgYEAg94u8haGl2n8pBiIlPDC15brdFGF0jZAQQjl0/t+N/jzNe0HOy1xGaN8SB4LrWBdoXCL0ZU/abOU+Nlnmpr9bq+Y4ynoC9GDXK6nM6hBYL0UQ5p2AV9LYKqXOOLzJy3HcLWS2qFpM28+4oKS3A/1V4677pWdH40PPxgZIc1sE68CgYEAiMZpGH+VNrLyYEoGF01Pp+G1Xiaekof4HEnzQE0m/H3jM4KNT/i4CNYCsLdX+AcvBhHFjV+Bj25MB3QS0U13HxGk/dm5KtIELx/W18twfV2Dpqnss5XEifJv+SiYD/cgwL2ggwTtDVraaBSti1wgkZhvAAcLeHlTuK5pMy85lIkCgYAwBes82L09SSjrrc+XxjJ/BwRKgSHmE0HFLXNdhtpJkg0ol2CpyXvF5YbuUJraEh0nHPCea+cDHJZlJe+YG1euRp68I9fQ7gUT7LsRkfX1QAawNAFDsKun0Zhu7B+3JfHa9xU7L8s7IwlYvkqzf9BOibmhQrVM9zcVODZ22/O/GwKBgGWd1zymW1NypAaxPDI5BxKXa9/oGw8efVSf5mlh1Gr0lW+lEsnSGh7zcWN9l6VkJTxz7rFmVb0oybtY+VHforTvdBxbBhxfGHeazj//o0roJVraeMy6Dl8ujdEL0WGOTLYORfD3ACVFzLQjbFAey4K/ERa88grLP2u8ybKAY1B9", // 私钥
            signType: 'RSA2', // 签名类型
            //公
            alipayPublicKey: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgvssaeiTU3u0U9cpoRY2dHv0iv+ogw/vYHPhkxU5FbdGbpUk+Pqyo8IZbAZgOFZO/cOvW9zrH05DyJxB0LLvePImL1iK8xgjR14EUQ3mx9Tv2l0pXQUBKTWiCQlSgK0cTQhfZOWH4WzeFgUcgXTBSJSfSPeX4soS9VErFLhrmnT2BT8EBgtH6MN3y78mTIWK19TgThAJE+s8DPqN/1K2DenhXuSBU/7KJVURNWdGPQRKyVjTIO4WgjTONHC/LAykVjyLSQO9bB7hUKxDrNl2ZHYyK7i6N5spsiEd3mXwCFPxZVtiPcm6PcQ+T1p3xQ+YAmQ9YlrNEl0f+dsMyDqGJQIDAQAB', // 支付宝公钥（不是应用公钥）
            gateway: 'https://openapi.alipaydev.com/gateway.do', // 网关地址
            timeout: 5000, // 网关超时时间
            camelcase: true // 是否把网关返回的下划线 key 转换为驼峰写法
        })

        const formData = new AlipayFormData();

        formData.setMethod('get'); //请求方式
        formData.addField('appId', '2021000118645860'); //用户id
        formData.addField('charset', 'utf-8'); //编码格式
        formData.addField('signType', 'RSA2');//注册信息格式
        formData.addField('bizContent', { //设置商户信息
            outTradeNo: new Date() * 1 + '', // 【必选】商户订单号：64个字符内，包含数字，字母，下划线；需要保证在商户端不重复
            productCode: 'FAST_INSTANT_TRADE_PAY', // 【必选】销售产品码(二维码)，目前仅支持FAST_INSTANT_TRADE_PAY
            totalAmount: `${total}`, // 【必选】订单总金额，精确到小数点后两位
            subject: '一个山东人', // 【必选】 订单标题
            body: 'sssssssssssssssss' // 【可选】订单描述
        })
        console.log('formData:===============', formData)
        /**
         * exec对应参数：
         * method（调用支付宝api）
         * params（api请求的参数（包含“公共请求参数”和“业务参数”））
         * options（validateSign，formData，log）
         *
         * 
         * 统一下单支付 exec
         * 买家账号icajqi9234@sandbox.com
         *登录密码111111
         *支付密码111111
         */
        ctx.body = await alipaySdk.exec('alipay.trade.page.pay', {}, { formData })
    }
}

module.exports = PayController;